import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_node_list.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/works/public/review_submitted.dart';

import '../../../../http/bean/ywx_limit.dart';
import '../../helper/route_helper.dart';
import '../../worksdo/type16_material_requisition/public/items/ui_attachment.dart';
import '../public/items/ui_payment.dart';
import 'acom/ui_purcontract_audit_form.dart';
import 'item/ui_purcontract_audit.dart';

///采购合同审核详情
class PurContractAuditDetailsRoute extends StatefulWidget {
  YwxItem item;

  PurContractAuditDetailsRoute(this.item);

  @override
  _State createState() => _State();
}

class _State extends State<PurContractAuditDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter.getContractapplygetinfoWithDelay(context); //获取审核详情数据
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body);
  }

  Widget _body(BuildContext context) {
    if (presenter?.mDatas == null) {
      return AppUi.getEmptyView();
    }

    var mDatas = presenter?.mDatas?.PurContract;
    var mOldDatas = presenter?.mOldDatas?.PurContractCompare; //旧数据，用于对比。
    //mDatas?.PurContractList?.first?.isChange();//fixme 如果 PurContractList长度为空，如："PurContractList": [],；first会报错。亲测。
    if (mDatas?.isChange() ?? false) {
      mOldDatas = null; //fixme 变更的时候，不做新旧数据判断；就只做物料信息里的 品牌  折算面积单价 单价 质量等级；
    }
    ///正常请购单审核
    List<Widget> children = [];
    children.add(UiPurContractAudit.build(
        context, widget.item, mDatas, mOldDatas, setState)); //基本信息
    children.add(UiPayment.build3(context, mDatas?.Cptc,
        oldData: mOldDatas?.Cptc)); //付款方式
    children.add(UiPurContractAuditForm.build(context, mDatas, widget.item,
        oldData: mOldDatas)); //物料信息
    // print("${mDatas?.getFileInfo().toString()}");
    // print("${mOldDatas?.getFileInfo().toString()}");
    bool titleRed = false;
    var fileInfo = mDatas?.getFileInfo().toString();
    var oldFileInfo = mOldDatas?.getFileInfo().toString();
    if (oldFileInfo != null && fileInfo != oldFileInfo) {
      titleRed = true;
    }
    children.add(UiAttachment.build(context, mDatas?.getFileInfo(),
        isShowEmpty: true,
        title: AppStrings.of(context).hetongfujian,
        titleRed: titleRed)); //合同附件
    children.add(UiNoteList.build(context, presenter?.mDatas?.NodeList)); //审核日志
    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(
            child: ReviewSubmittedStateful(
              presenter?.mDatas,
              hasBack: true,
              onlyBack: false,
              hasEnd: false,
              hasAuditOpinion: true,
              callbackWithT: (subData) {
                //fixme 提交参数数据
                // subData.GetCheckPurDemand = mDatas?.PurDemandList;
                // print(
                //     "========================================================入库仓位：${mDatas?.Storehouse}");
                // print(
                //     "========================================================检验员：${mDatas?.Checker}");
                // print(
                //     "========================================================入库仓位：${subData.FLoseApplyInfo?.Storehouse}");
                // print(
                //     "========================================================检验员：${subData.FLoseApplyInfo?.Checker}");
              },
            ),
            visible: presenter?.mDatas != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {}
